package com.group01.web.controller;

import com.group01.annotation.Status;
import com.group01.dto.Commodity.CommodityInsertInfoDTO;
import com.group01.dto.Commodity.CommodityUpdateInfoDTO;
import com.group01.result.PageTemplate;
import com.group01.result.Result;
import com.group01.service.CommodityService;
import com.group01.vo.Commodity.CommodityGetInfoByCateIdVO;
import com.group01.vo.Commodity.CommodityGetInfoByIdVO;
import com.group01.vo.Commodity.CommodityGetPageVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.DecimalMin;
import java.util.List;

/**
 * 图书管理（商品）
 * @Description
 * @Author 翟玉龙
 * @Date 2024/6/21
 */
@RestController
@RequestMapping("/book")
@Validated
@Api(tags = "图书管理（商品）模块")
public class CommodityController {

    /**
     * 注入商品服务
     */
    private final CommodityService commodityService;

    public CommodityController(CommodityService commodityService) {
        this.commodityService = commodityService;
    }

    /**
     * 分页 + 条件查询图书（商品）信息
     *
     * @param pageNum    页码
     * @param pageSize   页面大小
     * @param categoryId 分类id
     * @param query 查询条件
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("分页 + 条件查询图书（商品）信息")
    @GetMapping("/{pageNum}/{pageSize}")
    public Result<PageTemplate<CommodityGetPageVO>> getPageCommodity(
            @PathVariable Integer pageNum,
            @PathVariable Integer pageSize,
            @RequestParam(value = "categoryId" , required = false) Integer categoryId,
            @RequestParam(value = "query", required = false) String query
    ) {
        return commodityService.getPage(pageNum, pageSize, categoryId, query);
    }

    /**
     * 根据分类ID获取图书信息
     * @param categoryId 分类id
     * @return {@link Result }<{@link List }<{@link CommodityGetInfoByCateIdVO }>>
     */
    @ApiOperation("根据分类ID获取图书（商品）信息")
    @GetMapping("/getBooksByCategoryId/{categoryId}")
    public Result<List<CommodityGetInfoByCateIdVO>> getBooksByCategoryId(@PathVariable Integer categoryId) {
        return commodityService.getBooksByCategoryId(categoryId);
    }

    /**
     * 根据图书ID获取图书信息
     * @param id 图书id
     * @return {@link Result }<{@link CommodityGetInfoByIdVO }>
     */
    @ApiOperation("根据图书ID获取图书（商品）信息")
    @GetMapping("/{id}")
    public Result<CommodityGetInfoByIdVO> getCommodityById(@PathVariable Integer id) {
        return commodityService.getCommodityById(id);
    }

    /**
     * 管理端修改图书信息数据回显
     * @param id 图书id（商品）
     * @return {@link Result }<{@link CommodityGetInfoByIdVO }>
     */
    @ApiOperation("管理端修改图书信息数据回显")
    @GetMapping("/backend/{id}")
    public Result<CommodityGetPageVO> getCommodityByIdForBackend(@PathVariable Integer id) {
        return commodityService.getCommodityByIdForBackend(id);
    }

    /**
     * 新增图书
     * @param commodityInsertInfoDTO 新增图书传输数据DTO
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("新增图书（商品）")
    @PostMapping
    public Result<String> addCommodity(@RequestBody @Validated CommodityInsertInfoDTO commodityInsertInfoDTO) {
        return commodityService.addCommodity(commodityInsertInfoDTO);
    }

    /**
     * 修改图书状态
     * @param id 图书id（商品）
     * @param status 状态值
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改图书（商品）状态")
    @PutMapping("/{id}/{status}")
    public Result<String> updateCommodityStatus(@PathVariable @DecimalMin(value = "0" , message = "传入的id必须是数字且大于0") Integer id, @PathVariable @Status Integer status) {
        return commodityService.updateCommodityStatus(id, status);
    }

    /**
     * 修改图书
     * @param commodityUpdateInfoDTO 修改图书传输数据DTO
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("修改图书（商品）")
    @PutMapping
    public Result<String> updateCommodity(@RequestBody @Validated CommodityUpdateInfoDTO commodityUpdateInfoDTO) {
        return commodityService.updateCommodity(commodityUpdateInfoDTO);
    }

    /**
     * 根据图书ID删除图书信息
     * @param id 图书id（商品）
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("根据图书ID删除图书（商品）信息")
    @DeleteMapping("/{id}")
    public Result<String> deleteCommodity(@PathVariable @DecimalMin(value = "0", message = "id的值必须是数字且大于0") Integer id) {
        return commodityService.deleteCommodityById(id);
    }

    /**
     * 批量删除图书（商品）信息
     * @param ids 图书（商品）id集合
     * @return {@link Result }<{@link String }>
     */
    @ApiOperation("批量删除图书（商品）信息")
    @DeleteMapping
    public Result<String> deleteCommodityByIds(@RequestBody List<Integer> ids) {
        return commodityService.deleteCommodityByIds(ids);
    }
}
